package org.jeecg.modules.xxgx.hzsqgl.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.codec.Base64;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.modules.common.util.MathUtil;
import org.jeecg.modules.innerApi.common.requestDto.HzxxbcxParam;
import org.jeecg.modules.innerApi.common.requestDto.HzxxbtbParam;
import org.jeecg.modules.innerApi.common.requestDto.HzxxbxzParam;
import org.jeecg.modules.innerApi.common.requestDto.QdpHzxxbqrParam;
import org.jeecg.modules.innerApi.common.responseDto.FileStreamRtn;
import org.jeecg.modules.xxgx.common.enums.FplxdmEnum;
import org.jeecg.modules.xxgx.common.enums.HsbzEnum;
import org.jeecg.modules.xxgx.common.enums.HzclztEnum;
import org.jeecg.modules.xxgx.common.enums.HzhcztEnum;
import org.jeecg.modules.xxgx.common.util.CommonUtil;
import org.jeecg.modules.xxgx.hzsqgl.entity.CsXxHzsqjs;
import org.jeecg.modules.xxgx.hzsqgl.entity.CsXxHzsqjsmx;
import org.jeecg.modules.xxgx.hzsqgl.service.ICsXxHzsqjsService;
import org.jeecg.modules.xxgx.hzsqgl.service.ICsXxHzsqjsmxService;
import org.jeecg.modules.xxgx.hzsqgl.vo.CsXxHzsqjsPage;
import org.jeecg.modules.xxgx.hzsqgl.vo.CsXxHzsqjsPrint;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;


/**
* @Description: 销项发票红字申请管理扩展功能
* @Author: jeecg-boot
* @Date:   2023-09-15
* @Version: V1.0
*/
@Api(tags="销项发票红字申请管理")
@RestController
@RequestMapping("/xxgx/hzsqglkz")
@Slf4j
public class CsXxHzsqjsKzController {
   @Autowired
   private ICsXxHzsqjsService csXxHzsqjsService;
   @Autowired
   private ICsXxHzsqjsmxService csXxHzsqjsmxService;


    /**
     *   红字信息表申请
     *
     * @param page
     * @return
     */
    @AutoLog(value = "销项红字申请管理-红字信息表申请")
    @ApiOperation(value="销项红字申请管理-红字信息表申请", notes="销项红字申请管理-红字信息表申请")
    @PostMapping(value = "/hzxxbsq")
    @ResponseBody
    public Result<String> hzxxbsq(@RequestBody CsXxHzsqjsPage page) {
        return csXxHzsqjsService.hzxxbsq(page);
    }

    /**
     *   红字信息表同步
     *
     * @param params
     * @return
     */
    @AutoLog(value = "销项红字申请管理-红字信息表同步")
    @ApiOperation(value="销项红字申请管理-红字信息表同步", notes="销项红字申请管理-红字信息表同步")
    @PostMapping(value = "/hzxxbtb")
    public Result<String> hzxxbtb(@RequestBody HzxxbtbParam params) {
        return csXxHzsqjsService.hzxxbtb(params);
    }

    /**
     *   红字信息表撤销
     *
     * @param params
     * @return
     */
    @AutoLog(value = "销项红字申请管理-红字信息表撤销")
    @ApiOperation(value="销项红字申请管理-红字信息表撤销", notes="销项红字申请管理-红字信息表撤销")
    @PostMapping(value = "/hzxxbcx")
    public Result<String> hzxxbcx(@RequestBody HzxxbcxParam params) {
        return csXxHzsqjsService.hzxxbcx(params);
    }
    /**
     *   红字信息表上传
     *
     * @param params
     * @return
     */
    @AutoLog(value = "销项红字申请管理-红字信息表上传")
    @ApiOperation(value="销项红字申请管理-红字信息表上传", notes="销项红字申请管理-红字信息表上传")
    @PostMapping(value = "/hzxxbsc")
    public Result<String> hzxxbsc(@RequestBody HzxxbcxParam params) {
        return csXxHzsqjsService.hzxxbsc(params);
    }
    /**
     *   红字信息表下载
     *
     * @param params
     * @return
     */
    @AutoLog(value = "销项红字申请管理-红字信息表下载")
    @ApiOperation(value="销项红字申请管理-红字信息表下载", notes="销项红字申请管理-红字信息表下载")
    @PostMapping(value = "/hzxxbxz")
    public void hzxxbxz(@RequestBody HzxxbxzParam params, HttpServletResponse response) {
        try {
            FileStreamRtn fileStreamRtn = csXxHzsqjsService.hzxxbxz(params);
            String fileName=fileStreamRtn.getContentDisposition();
            if(fileName!=null) {
                response.setHeader("Content-Disposition", "filename=" + fileName);
            }
            response.setContentType("application/octet-stream;charset=utf-8");

            InputStream in = fileStreamRtn.getFileStream();
            OutputStream out = response.getOutputStream();
            // 循环取出流中的数据
            byte[] b = new byte[100];
            int len;
            while ((len = in.read(b)) != -1) {
                out.write(b, 0, len);
            }
            in.close();
            out.close();
            response.flushBuffer();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     *   全电红字信息表确认
     *
     * @param params
     * @return
     */
    @AutoLog(value = "销项红字申请管理-全电红字信息表确认")
    @ApiOperation(value="销项红字申请管理-全电红字信息表确认", notes="销项红字申请管理-全电红字信息表确认")
    @PostMapping(value = "/hzxxbqr")
    public Result<String> hzxxbqr(@RequestBody QdpHzxxbqrParam params) {
        return csXxHzsqjsService.hzxxbqr(params);
    }


    @ApiOperation(value="红字申请-通过发票代码号码查询红字申请", notes="红字申请-通过发票代码号码查询红字申请")
    @GetMapping(value = "/queryHzxxb")
    public Result<CsXxHzsqjs> queryHzxxb(@RequestParam(name="yfplxdm",required=true) String yfplxdm,
                                         @RequestParam(name="yfpdm",required=false) String yfpdm,
                                         @RequestParam(name="yfphm",required=true) String yfphm) {
        LambdaQueryWrapper<CsXxHzsqjs> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //兼容前端传递中文发票类型问题
        if(StringUtils.isNotEmpty(yfplxdm)){
            if(CommonUtil.isChinese(yfplxdm)){
                yfplxdm=FplxdmEnum.getValueByDesc(yfplxdm);
            }
        }
        lambdaQueryWrapper.eq(CsXxHzsqjs::getYfplxdm,yfplxdm);
        if(FplxdmEnum.sdzp.getValue().equals(yfplxdm) || FplxdmEnum.sdpp.getValue().equals(yfplxdm)) {
            lambdaQueryWrapper.eq(CsXxHzsqjs::getHzzt, HzhcztEnum.zt04.getValue());
        } else if(FplxdmEnum.zzszp.getValue().equals(yfplxdm) || FplxdmEnum.dzzp.getValue().equals(yfplxdm)) {
            lambdaQueryWrapper.eq(CsXxHzsqjs::getYfpdm,yfpdm);
            lambdaQueryWrapper.eq(CsXxHzsqjs::getClzt, HzclztEnum.zt2.getValue());
        } else {
            return Result.error("该发票类型无红字信息");
        }
        lambdaQueryWrapper.eq(CsXxHzsqjs::getYfphm,yfphm);
        lambdaQueryWrapper.orderByDesc(CsXxHzsqjs::getSqsj);
        lambdaQueryWrapper.last("limit 1");

        CsXxHzsqjs csXxHzsqjs = csXxHzsqjsService.getOne(lambdaQueryWrapper,false);
        if(csXxHzsqjs == null) {
            return Result.error("无红字信息");
        }
        return Result.OK(csXxHzsqjs);

    }

    @AutoLog(value = "红字信息表打印数据格式封装")
    @ApiOperation(value="红字信息表打印数据格式封装", notes="红字信息表打印数据格式封装")
    @RequestMapping(value = "/getHzsqjsDy", method = {RequestMethod.GET})
    public Result<String> getHzsqjsDy(String id) {
        CsXxHzsqjsPrint csXxHzsqjsPrint = new CsXxHzsqjsPrint();
        CsXxHzsqjs csXxHzsqjs = csXxHzsqjsService.getById(id);
        if(csXxHzsqjs == null ){
            return Result.error("未找到对应数据");
        }
        List<CsXxHzsqjsmx> mxlist = csXxHzsqjsmxService.selectByMainId(id);

        BeanUtils.copyProperties(csXxHzsqjs, csXxHzsqjsPrint);
        csXxHzsqjsPrint.setXxbbh(csXxHzsqjs.getXxbbh());
        csXxHzsqjsPrint.setSqdh(csXxHzsqjs.getXxbbh());
        csXxHzsqjsPrint.setGfsh(csXxHzsqjs.getKhsh());
        csXxHzsqjsPrint.setGfmc(csXxHzsqjs.getKhmc());
        csXxHzsqjsPrint.setXfsh(csXxHzsqjs.getXfsh());
        csXxHzsqjsPrint.setXfmc(csXxHzsqjs.getXfmc());
        csXxHzsqjsPrint.setHjje(csXxHzsqjs.getHjje());
        csXxHzsqjsPrint.setHjse(csXxHzsqjs.getHjse());
        csXxHzsqjsPrint.setSe(csXxHzsqjs.getHjse());
        csXxHzsqjsPrint.setJshj(MathUtil.add(csXxHzsqjs.getHjje(), csXxHzsqjs.getHjse()));
        csXxHzsqjsPrint.setSqfs(csXxHzsqjs.getHclx());
        csXxHzsqjsPrint.setLxdh(csXxHzsqjs.getLxdh());
        csXxHzsqjsPrint.setNsrsbh(csXxHzsqjs.getXfsh());
        csXxHzsqjsPrint.setSqrq(csXxHzsqjs.getSqsj());
        csXxHzsqjsPrint.setKprq(csXxHzsqjs.getYkprq());
        csXxHzsqjsPrint.setFpdm(csXxHzsqjs.getYfpdm());
        csXxHzsqjsPrint.setFphm(csXxHzsqjs.getYfphm());
        csXxHzsqjsPrint.setKpddm(csXxHzsqjs.getKpzddm());
        csXxHzsqjsPrint.setZsfs(csXxHzsqjs.getZsfs());
        csXxHzsqjsPrint.setScbz(csXxHzsqjs.getSczt());
        csXxHzsqjsPrint.setSqfjbr(csXxHzsqjs.getSqr());

        //申请方：00：购方申请；01：购方申请-已抵扣；02：购方申请-未抵扣；10：销方申请
        String sqf = csXxHzsqjs.getSqf();
        //sqly、sqfs这两个字段打印组件中貌似使用的是sqfs作为sqf字段使用
        if("01".equals(sqf)){
            csXxHzsqjsPrint.setSqly("0");
            csXxHzsqjsPrint.setSqfs("0");
        } else if("02".equals(sqf)){
            csXxHzsqjsPrint.setSqly("1");
            csXxHzsqjsPrint.setSqfs("1");
        } else if("10".equals(sqf)){
            csXxHzsqjsPrint.setSqly("2");
            csXxHzsqjsPrint.setSqfs("2");
        }

        String hcyydm = csXxHzsqjs.getHcyy();
        if("01".equals(hcyydm)){
            csXxHzsqjsPrint.setChyydm("0");
        } else if("02".equals(hcyydm)){
            csXxHzsqjsPrint.setChyydm("1");
        } else if("03".equals(hcyydm)){
            csXxHzsqjsPrint.setChyydm("2");
        } else if("04".equals(hcyydm)){
            csXxHzsqjsPrint.setChyydm("3");
        }

        List<CsXxHzsqjsPrint.Hzsqjsmx> hzsqmxPrintList = new ArrayList<>();
        mxlist.forEach(csXxHzsqjsmx -> {
            CsXxHzsqjsPrint.Hzsqjsmx hzsqjsmx = new CsXxHzsqjsPrint.Hzsqjsmx();
            BeanUtils.copyProperties(csXxHzsqjsmx, hzsqjsmx);
            hzsqjsmx.setSqlsh(csXxHzsqjs.getSqlsh());
            if(HsbzEnum.Y.getValue().equals(csXxHzsqjs.getHsbz())){
                hzsqjsmx.setDj(csXxHzsqjsmx.getHsdj());
                hzsqjsmx.setJe(csXxHzsqjsmx.getHsje());
            } else {
                hzsqjsmx.setDj(csXxHzsqjsmx.getBhsdj());
                hzsqjsmx.setJe(csXxHzsqjsmx.getBhsje());
            }
            hzsqjsmx.setSe(csXxHzsqjsmx.getTaxje());
            hzsqjsmx.setZxbm(csXxHzsqjsmx.getSpdm());
            hzsqmxPrintList.add(hzsqjsmx);
        });
        csXxHzsqjsPrint.setFpmx(hzsqmxPrintList);

        //转base64
        String xmlBase64 = Base64.encodeToString(JSON.toJSONString(csXxHzsqjsPrint).getBytes());
        return Result.OK(xmlBase64);
    }

}
